package com.iims.flowable.flow.vo.resp;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import java.util.Date;
import java.util.Objects;

/**
 * 字段属性处理小助手
 */
public class FlowFiledUtils {

    /**
     * 流程完成时间处理
     *
     * @param ms
     * @return
     */
    public static String getDate(long ms) {

        long day = ms / (24 * 60 * 60 * 1000);
        long hour = (ms / (60 * 60 * 1000) - day * 24);
        long minute = ((ms / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long second = (ms / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - minute * 60);

        if (day > 0) {
            return day + "天" + hour + "小时" + minute + "分钟";
        }
        if (hour > 0) {
            return hour + "小时" + minute + "分钟";
        }
        if (minute > 0) {
            return minute + "分钟";
        }
        if (second > 0) {
            return second + "秒";
        } else {
            return 0 + "秒";
        }
    }

    public static String getDate(Long ms) {
        if (null == ms)
            return "";
        else
            return getDate(ms.longValue());

    }

    /**
     * 组装名称带时间
     *
     * @param name
     * @param date
     * @return
     */
    public static String getNameWithDate(String name, Date date) {
        return StrUtil.join("", name, "[", DateUtil.format(date, "yyyy-MM-dd HH:mm:ss"), "]");
    }

    /**
     * 计算持续时间
     *
     * @param startTime 起始时间
     * @param endTime   结束时间
     * @return
     */
    public static String caclDuration(Date startTime, Date endTime) {
        if (null == startTime) {
            return "";
        }
        // 计算耗时
        long time;
        if (Objects.nonNull(endTime)) {
            time = endTime.getTime() - startTime.getTime();
        } else {
            time = System.currentTimeMillis() - startTime.getTime();
        }
        return getDate(time);
    }
}
